/*
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package hextd.network;

import java.io.BufferedReader;
import java.io.IOException;

/**
 *
 * @author hylke
 */
public class InputWaiter implements Runnable {

    private BufferedReader inBuffer;
    private LineListener client;
    private boolean running = true;

    public InputWaiter(BufferedReader in, LineListener client) {
        this.inBuffer = in;
        this.client = client;
    }

    public void end() {
        try {
            this.running = false;
            this.inBuffer.close();
        } catch (IOException ex) {
        }
    }
    
	@Override
    public void run() {
        try {
            String inputLine;
            while ((inputLine = this.inBuffer.readLine()) != null && this.running) {
                this.client.lineReceived(inputLine);
            }
        } catch (IOException ex) {
            System.err.println("InputWaiter::run: connection closed.");
        }
        this.client.inputStopped();
    }
}
